<template>
  <div v-if="busiKey != undefined && loadFinish">
    <div class="add_form_label_title">{{ busiType == 'thirder' ? '【新增第三人】' : '【涉及材料】' }}</div>
    <ApplyCaseHandleProcedure
      v-if="
        busiType == 'accept' &&
        busiKey != Tp_proctypeEnum.accept_change_firstper.value &&
        busiKey != Tp_proctypeEnum.accept_change_secondper.value
      "
      ref="applyCaseHandleProcedure"
      :applyid="applyid"
      :busiKey="busiKey"
      :category="categoryVal"
      btnLabel=""
      :isEditModel="isEditModel"
      processinstanceid=""
      @reload="initParam"
    />
    <ChangePersonProcedure
      v-if="
        busiType == 'accept' &&
        (busiKey == Tp_proctypeEnum.accept_change_firstper.value ||
          busiKey == Tp_proctypeEnum.accept_change_secondper.value)
      "
      ref="changePersonProcedure"
      :applyid="applyid"
      :busiKey="busiKey"
      :category="categoryVal"
      btnLabel=""
      :isEditModel="isEditModel"
      processinstanceid=""
      @reload="initParam"
    />

    <ExecuteHandleProcedure
      v-if="busiType == 'execute'"
      ref="executeHandleProcedure"
      :applyid="applyid"
      :busiKey="busiKey"
      :category="categoryVal"
      btnLabel=""
      :isEditModel="isEditModel"
      processinstanceid=""
      :sonStatus="sonStatus"
      :admreview="admreview"
      @reload="initParam"
    />

    <CaseHandleProcedure
      v-if="
        busiType == 'hear' &&
        busiKey != Tp_proctypeEnum.hear_change_firtper.value &&
        busiKey != Tp_proctypeEnum.hear_change_secondper.value
      "
      ref="caseHandleProcedure"
      :applyid="applyid"
      :busiKey="busiKey"
      :category="categoryVal"
      btnLabel=""
      :isEditModel="isEditModel"
      processinstanceid=""
      :sonStatus="sonStatus"
      @reload="initParam"
    />

    <CaseHandleChangePerson
      v-if="
        busiType == 'hear' &&
        (busiKey == Tp_proctypeEnum.hear_change_firtper.value || busiKey == Tp_proctypeEnum.hear_change_secondper.value)
      "
      ref="caseHandleChangePerson"
      :applyid="applyid"
      :busiKey="busiKey"
      :category="categoryVal"
      btnLabel=""
      :isEditModel="isEditModel"
      :sonStatus="sonStatus"
      processinstanceid=""
      @reload="initParam"
    />

    <CaseHandleDecision
      v-if="busiType == 'decision'"
      ref="caseHandleDecision"
      :applyid="applyid"
      :busiKey="busiKey"
      :category="categoryVal"
      btnLabel=""
      :isEditModel="isEditModel"
      processinstanceid=""
      :sonStatus="sonStatus"
      :decisiontype="decisiontype"
      @reload="initParam"
    >
    </CaseHandleDecision>

    <!-- 当前为第三人审批流程 -->
    <ThirdPersonProcessMaterial
      v-if="busiType == 'thirder'"
      :applyid="applyid"
      :category="category"
      :isEditModel="isEditModel"
      :processinstanceid="processinstanceid"
      :sonStatus="sonStatus"
    >
    </ThirdPersonProcessMaterial>
  </div>
</template>

<script>
import { getSonStatusByIds } from '@/api/accept/apl_admreview'
import { ModelKey, ACCEPT_ARRAY, HEAR_ARRAY, EXECUTE_ARRAY } from '@/enumeration/ModelKey.js'
import { viewApl_admre_audit } from '@/api/accept/apl_admre_audit'
import { Tp_proctypeValue, Tp_proctypeEnum } from '@/enumeration/Tp_proctype.js'
import { getProcessVariable, getHandleParams } from '@/api/workflow/wf_ProcessInstance.js'
export default {
  // mixins: [ProcedureMinxin],
  props: {
    applyid: {
      type: String,
      required: true
    },
    modelKey: {
      type: String,
      required: true
    },
    category: {
      // 流程状态
      type: String,
      required: false
    },
    isEditModel: {
      // 是否是编辑模式
      type: Boolean,
      default: false,
      required: true
    }
  },
  data () {
    return {
      busiKey: undefined, // 业务环节
      busiType: undefined,
      typeEnum: undefined, // 类型枚举
      fileItem: [],
      ModelKey: ModelKey,
      categoryVal: undefined,
      sonStatus: undefined, // 子流程
      decisiontype: undefined, // 决定类型
      admreview: undefined, // 主记录数据
      Tp_proctypeEnum: Tp_proctypeEnum,
      loadFinish: false // 加载完成的标识字段
    }
  },
  created () {
    this.initParam()
  },
  methods: {
    async initParam () {
      const _this = this
      this.busiType = undefined
      this.busiKey = undefined
      this.loadFinish = false
      try {
        // 通过传入的流程环节来得到对应的业务环节
        const res = await getSonStatusByIds({ applyid: this.applyid })
        if (res.code !== 0) {
          this.$message.error('查询子流程失败')
        } else {
          this.sonStatus = res.data.sonStatus
          const res3 = await getProcessVariable(this.$route.params.processinstanceid)
          let variableMap = {}
          if (res3.code == 0) {
            variableMap = res3.data || {}
          }
          this.decisiontype = res.data.decisiontypeid || variableMap.decisiontypeid // 决定类型
          this.admreview = res.data.admreview // 主表记录
        }
        if (this.ModelKey && this.modelKey && this.ModelKey[this.modelKey]) {
          this.busiKey = this.ModelKey[this.modelKey].busiKey
          this.categoryVal = this.category || this.ModelKey[this.modelKey].categoryValue
          // this.labelTitle = this.ModelKey[this.modelKey].name
          if (ACCEPT_ARRAY.includes(this.modelKey)) {
            const res1 = await viewApl_admre_audit(this.$route.params.applyid)
            if (res1.code == 0) {
              if (res1.data && res1.data.type == Tp_proctypeValue.ACCEPT_CHANGE_FIRSTPER) {
                this.busiKey = this.Tp_proctypeEnum.accept_change_firstper.value
              } else if (res1.data && res1.data.type == Tp_proctypeValue.ACCEPT_CHANGE_SECONDPER) {
                this.busiKey = this.Tp_proctypeEnum.accept_change_secondper.value
              }
            }
            this.busiType = 'accept'
          } else if (EXECUTE_ARRAY.includes(this.modelKey)) {
            const res1 = await viewApl_admre_audit(this.$route.params.applyid)
            if (res1.code == 0) {
              if (res1.data && res1.data.type == Tp_proctypeValue.EXECUTE_FORCE_LAW) {
                this.busiKey = this.Tp_proctypeEnum.execute_force_law.value
              }
            }
            this.busiType = 'execute'
          } else {
            this.busiType = 'hear'
            if (this.modelKey == 'HEAR_DECISION') {
              this.busiType = 'decision'
            } else if (this.modelKey == 'HEAR_THIRDER') {
              this.busiType = 'thirder'
            }
            const res1 = await viewApl_admre_audit(this.$route.params.applyid)
            if (res1.code == 0) {
              if (res1.data && res1.data.type == Tp_proctypeValue.HEAR_CHANGE_FIRTPER) {
                this.busiKey = this.Tp_proctypeEnum.hear_change_firtper.value
              } else if (res1.data && res1.data.type == Tp_proctypeValue.HEAR_CHANGE_SECONDPER) {
                this.busiKey = this.Tp_proctypeEnum.hear_change_secondper.value
              }
            }
          }
        }
      } finally {
        this.loadFinish = true
      }
    }
  },
  components: {
    CaseHandleProcedure: () => import('@/views/handleCase/accept/CaseHandleProcedure.vue'),
    CaseHandleDecision: () => import('@/views/handleCase/accept/CaseHandleDecision.vue'),
    ApplyCaseHandleProcedure: () => import('@/views/handleCase/apply/ApplyCaseHandleProcedure.vue'),
    ExecuteHandleProcedure: () => import('@/views/handleCase/execute/ExecuteHandleProcedure.vue'),
    ThirdPersonProcessMaterial: () => import('@/views/accept/material/ThirdPersonProcessMaterial.vue'),
    ChangePersonProcedure: () => import('@/views/handleCase/apply/ChangePersonProcedure.vue'),
    CaseHandleChangePerson: () => import('@/views/handleCase/accept/CaseHandleChangePerson.vue')
  }
}
</script>

<style lang="scss" scoped>
@import '~@/assets/css/apply/caseProcedure.scss';

.add_form_label_title {
  text-align: center;
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 20px;
}
</style>
